﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace foldshot.View
{
    /// <summary>

    /// Represents a wildcard running on the

    /// <see cref="System.Text.RegularExpressions"/> engine.

    /// </summary>

    public class Wildcard : Regex
    {
        /// <summary>

        /// Initializes a wildcard with the given search pattern.

        /// </summary>

        /// <param name="pattern">The wildcard pattern to match.</param>

        public Wildcard(string pattern)
            : base(WildcardToRegex(pattern))
        {
        }

        /// <summary>

        /// Initializes a wildcard with the given search pattern and options.

        /// </summary>

        /// <param name="pattern">The wildcard pattern to match.</param>

        /// <param name="options">A combination of one or more

        /// <see cref="RegexOptions"/>.</param>

        public Wildcard(string pattern, RegexOptions options)
            : base(WildcardToRegex(pattern), options)
        {
        }

        /// <summary>

        /// Converts a wildcard to a regex.

        /// </summary>

        /// <param name="pattern">The wildcard pattern to convert.</param>

        /// <returns>A regex equivalent of the given wildcard.</returns>

        public static string WildcardToRegex(string pattern)
        {
            return "^" + Regex.Escape(pattern).
             Replace("\\*", ".*").
             Replace("\\?", ".") + "$";
        }
    }
}
